include $(TOPDIR)/rules.mk

PKG_NAME:=docker-ce
PKG_VERSION:=19.03.3
PKG_RELEASE:=2
PKG_LICENSE:=Apache-2.0
PKG_LICENSE_FILES:=components/cli/LICENSE components/engine/LICENSE

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/docker/docker-ce/tar.gz/v$(PKG_VERSION)?
PKG_HASH:=63b0d28608f32573b9c03fa46247c6f959e9c08133ddf30a71276919de0194c0
PKG_SOURCE_VERSION:=a872fc2f86

PKG_MAINTAINER:=Gerard Ryan <G.M0N3Y.2503@gmail.com>

define CheckExpectedSrcVer
	$(eval SRC_VER:=$(shell grep --only-matching --perl-regexp '(?<=PKG_SOURCE_VERSION:=)(.*)' $(1)))
	$(if $(subst $(2),,$(SRC_VER)), \
		$(error ERROR: Expected $(1) source version '$(2)', found '$(SRC_VER)'), \
		$(info OK: Expected $(1) source version '$(2)', found '$(SRC_VER)') \
	)
endef

# values from respective '.installer' files at https://github.com/docker/docker-ce/blob/v$(PKG_VERSION)/components/engine/hack/dockerfile/install/
$(eval $(call CheckExpectedSrcVer,../containerd/Makefile,b34a5c8af56e510852c35414db4c1f4fa6172339))
$(eval $(call CheckExpectedSrcVer,../libnetwork/Makefile,45c710223c5fbf04dc3028b9a90b51892e36ca7f))
$(eval $(call CheckExpectedSrcVer,../runc/Makefile,3e425f80a8c931f88e6d94a8c831b9d5aa481657))
$(eval $(call CheckExpectedSrcVer,../tini/Makefile,fec3683b971d9c3ef73f284f176672c44b448662))

PKG_BUILD_DEPENDS:=golang/host
PKG_BUILD_PARALLEL:=1

GO_PKG:=github.com/docker

include $(INCLUDE_DIR)/package.mk
include ../../lang/golang/golang-package.mk

define Package/docker-ce/config
  source "$(SOURCE)/Config.in"
endef

define Package/docker-ce
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=Docker Community Edition
  URL:=https://www.docker.com/
  DEPENDS:=$(GO_ARCH_DEPENDS) @HAS_FPU @TARGET_x86_64||arm||aarch64 +btrfs-progs +ca-certificates +cgroupfs-mount +containerd +libdevmapper +libnetwork +tini \
           +DOCKER_SECCOMP:libseccomp +iptables-mod-extra +kmod-br-netfilter +kmod-ikconfig +kmod-nf-conntrack-netlink +kmod-nf-ipvs +kmod-veth \
           +@KERNEL_CGROUPS  +@KERNEL_CGROUP_CPUACCT +@KERNEL_CGROUP_SCHED
  USERID:=docker:docker

endef

define Package/docker-ce/description
  Docker Engine is used by millions enables containerized applications
  to run anywhere consistently on any infrastructure.
endef

define Build/Configure
	# move so GoPackage/Build/Configure will get the correct path
	mv $(PKG_BUILD_DIR)/components/engine $(PKG_BUILD_DIR)/
	mv $(PKG_BUILD_DIR)/components/cli $(PKG_BUILD_DIR)/

	# docker generates files at build time so we'll just symlink for now and call GoPackage/Build/Configure later
	mkdir -p $(GO_PKG_BUILD_DIR)/bin \
			 $(GO_PKG_BUILD_DIR)/src \
			 $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/ \
			 $(GO_PKG_CACHE_DIR) \
			 $(GO_PKG_TMP_DIR)
	$(LN) $(PKG_BUILD_DIR)/cli $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/cli
	$(LN) $(PKG_BUILD_DIR)/engine $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/docker
endef

ifeq ($(CONFIG_DOCKER_SECCOMP),y)
BUILDTAGS:=seccomp
else
BUILDTAGS:=
endif

define Build/Compile
	( \
		export GOPATH=$(GO_PKG_BUILD_DIR) \
			GOCACHE=$(GO_PKG_CACHE_DIR) \
			GOTMPDIR=$(GO_PKG_TMP_DIR) \
			GOROOT_FINAL=$(GO_TARGET_ROOT) \
			CC=$(TARGET_CC) \
			CXX=$(TARGET_CXX) \
			$(call GoPackage/Environment) \
			GITCOMMIT=$(PKG_SOURCE_VERSION) \
			DOCKER_GITCOMMIT=$(PKG_SOURCE_VERSION) \
			DOCKER_BUILDTAGS='$(BUILDTAGS)' \
			VERSION=$(PKG_VERSION) \
		\
		&& echo "Compiling CLI..." \
		&& cd $(PKG_BUILD_DIR)/cli \
		&& ./scripts/build/binary \
		\
		&& echo "Compiling Engine..." \
		&& cd $(PKG_BUILD_DIR)/engine \
		&& ./hack/make.sh binary \
	)

	# done here to include autogenerated files also
	rm $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/cli
	rm $(GO_PKG_BUILD_DIR)/src/$(GO_PKG)/docker
	$(call GoPackage/Build/Configure)
endef

define Package/docker-ce/install
	$(INSTALL_DIR) $(1)/usr/bin/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/cli/build/docker $(1)/usr/bin/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/engine/bundles/binary-daemon/dockerd $(1)/usr/bin/

	$(INSTALL_DIR) $(1)/opt/docker/
	$(INSTALL_DIR) $(1)/usr/share/docker/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/engine/contrib/check-config.sh $(1)/usr/share/docker/

	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/dockerd.init $(1)/etc/init.d/dockerd

	$(INSTALL_DIR) $(1)/etc/docker
	$(INSTALL_CONF) ./files/daemon.json $(1)/etc/docker/
endef

$(eval $(call BuildPackage,docker-ce))
